package com.llan;

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandKey;
import com.netflix.hystrix.HystrixThreadPoolKey;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
 * Created by java_long on 2018/5/18 18:22.
 */
public class HystrixHelloWorldCommand extends HystrixCommand<String> {
    private final String name;
    /**
     *这是是缓存Setter的示例,需要注释掉,否则命令名称会被覆盖,导致单元测试失败

    private static final Setter cachedSetter =
            Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("ExampleGroup"))
                    .andCommandKey(HystrixCommandKey.Factory.asKey("Cache Setter: Hello World"));
    public HystrixHelloWorldCommand(String name) {
        super(cachedSetter);
        this.name = name;
    }*/

    public HystrixHelloWorldCommand(String name) {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("Command Group: Hello World"))
                .andCommandKey(HystrixCommandKey.Factory.asKey("Command Name: Hello World"))
                .andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey("Command ThreadPool: Hello World")));
        this.name = name;
    }

    @Override
    protected String run() {
        return "Hello " + name + "!";
    }

    public static class UnitTest {

        @Test
        public void testCommandName() {
            assertEquals("Command Group: Hello World", new HystrixHelloWorldCommand("World").getCommandGroup().name());
            assertEquals("Command Name: Hello World", new HystrixHelloWorldCommand("World").getCommandKey().name());
            assertEquals("Command ThreadPool: Hello World", new HystrixHelloWorldCommand("World").getThreadPoolKey().name());
        }
    }
}

